package com.ctcemti.mdm.project.sys.controller;

import com.ctcemti.mdm.common.result.Result;
import com.ctcemti.mdm.common.result.ResultEnum;
import com.ctcemti.mdm.common.util.JWTUtil;
import com.ctcemti.mdm.project.sys.entity.ResMaterialLibrary;
import com.ctcemti.mdm.project.sys.service.IResMaterialLibraryService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * @ClassName ResMaterialLibraryController
 * @Description 物资目录库控制层
 * @Author bxf
 * @Date 8:32 2019/10/28
 * @Version V1.0
 **/

@RestController
@Api(value = "ResMaterialLibraryController", description = "物资目录库控制器类")
@RequestMapping("resMaterialLibrary")
public class ResMaterialLibraryController {
    @Autowired
    private IResMaterialLibraryService resMaterialLibraryService;

    @ApiOperation(value = "分页物资目录库")
    @PostMapping("selectListByPid")
    @ApiImplicitParam(name="resMaterialLibrary",value = "物资目录库实体类")
    public Result<PageInfo> selectListByPid(HttpServletRequest request, HttpServletResponse response , ResMaterialLibrary resMaterialLibrary){

        PageInfo<ResMaterialLibrary> pageInfo = resMaterialLibraryService.selectListByPid(resMaterialLibrary);

        return new Result<PageInfo>(ResultEnum.SUCCESS,pageInfo);
    }

    @ApiOperation(value = "保存物资目录库")
    @PostMapping("insertResMaterialLibrary")
    @ApiImplicitParam(name="resMaterialLibrary",value = "物资目录库实体类")
    public Result<ResMaterialLibrary> insertResMaterialLibrary(HttpServletRequest request, HttpServletResponse response , ResMaterialLibrary resMaterialLibrary){
        String loginId = JWTUtil.getLoginId(request.getHeader("Authorization"));
        resMaterialLibrary.setCreator(loginId);
        resMaterialLibraryService.insertSelective(resMaterialLibrary);
        return new Result<ResMaterialLibrary>(ResultEnum.SUCCESS,null);
    }

    @ApiOperation(value = "更新物资目录库")
    @PostMapping("updateResMaterialLibrary")
    @ApiImplicitParam(name="resMaterialLibrary",value = "物资目录库实体类")
    public Result<ResMaterialLibrary> updateResMaterialLibrary(HttpServletRequest request, HttpServletResponse response, ResMaterialLibrary resMaterialLibrary){
        String loginId = JWTUtil.getLoginId(request.getHeader("Authorization"));
        resMaterialLibrary.setModifiedBy(loginId);
        resMaterialLibrary.setModifyDate(new Date());
        resMaterialLibraryService.updateByIdSelective(resMaterialLibrary);
        return new Result<ResMaterialLibrary>(ResultEnum.SUCCESS,null);
    }

    @ApiOperation(value = "获取所有的物资目录库")
    @PostMapping("selectAllResMaterialLibrary")
    public Result<List<ResMaterialLibrary>> selectAllResMaterialLibrary(HttpServletRequest request, HttpServletResponse response){
        List<ResMaterialLibrary> list = new ArrayList<ResMaterialLibrary>();
        list = resMaterialLibraryService.selectAllResMaterialLibrary();
        return new Result<List<ResMaterialLibrary>>(ResultEnum.SUCCESS,list);
    }

    @ApiOperation(value = "根据物资目录库实体类获取list")
    @PostMapping("selectResMaterialLibraryByEntity")
    @ApiImplicitParam(name="resMaterialLibrary",value = "物资目录库实体类")
    public Result<ResMaterialLibrary> selectResMaterialLibraryByEntity(HttpServletRequest request, HttpServletResponse response,ResMaterialLibrary resMaterialLibrary){
        List<ResMaterialLibrary> list = new ArrayList<ResMaterialLibrary>();
        list = resMaterialLibraryService.selectResMaterialLibrary(resMaterialLibrary);
        return new Result<ResMaterialLibrary>(ResultEnum.SUCCESS,list);
    }

    @ApiOperation(value = "根据ids删除物资目录库")
    @PostMapping("deleteResMaterialLibraryByIds")
    @ApiImplicitParam(name="ResMaterialLibrary",value = "物资目录库实体类")
    public Result<ResMaterialLibrary> deleteResMaterialLibraryByIds(HttpServletRequest request, HttpServletResponse response,String ids){
        List<ResMaterialLibrary> list = new ArrayList<ResMaterialLibrary>();
        int flag = resMaterialLibraryService.deleteByPrimaryKeys(ids);
        return new Result<ResMaterialLibrary>(ResultEnum.SUCCESS,null);
    }

    @ApiOperation(value = "数据导入")
    @PostMapping("uploadFile")
    public Result<Map<String,Object>> uploadFile(HttpServletRequest request, HttpServletResponse response, String category) throws Exception {
        CommonsMultipartResolver commonsMultipartResolver =  new CommonsMultipartResolver(request.getSession().getServletContext());
        MultipartFile multipartFile = null;
        if(commonsMultipartResolver.isMultipart(request)){
            MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest)request;
            Iterator<String> iterator = multipartHttpServletRequest.getFileNames();
            while (iterator.hasNext()){
                multipartFile= multipartHttpServletRequest.getFile(iterator.next());
            }
        }
        Map<String,Object> result = resMaterialLibraryService.uploadFile(multipartFile,category,response,request);
        return new Result<Map<String,Object>>(ResultEnum.SUCCESS,result);
    }
}
